package com.xyht.medicine_management_system.modules.medicineSource.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicine;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineMapper;
import com.xyht.medicine_management_system.modules.medicineSource.entity.MmsMedicineSource;
import com.xyht.medicine_management_system.modules.medicineSource.entity.MmsMedicineSourceMedicineRelation;
import com.xyht.medicine_management_system.modules.medicineSource.entity.req.MmsMedicineSourceMedicineRelationReq;
import com.xyht.medicine_management_system.modules.medicineSource.entity.resp.MmsMedicineSourceAndMedicineRelationResp;
import com.xyht.medicine_management_system.modules.medicineSource.mapper.MmsMedicineSourceMapper;
import com.xyht.medicine_management_system.modules.medicineSource.mapper.MmsMedicineSourceMedicineRelationMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsOnConstant.MEDICINE_IS_OFF;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IdTypeConstant.ID_TYPE_MEDICINE;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IdTypeConstant.ID_TYPE_SOURCE;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IsDisplayConstant.IS_DISPLAY_FALSE;

@Component
public class MedicineSourceUtil {
    @Resource
    private MmsMedicineSourceMedicineRelationMapper relationMapper;
    @Resource
    private MmsMedicineSourceMapper mmsMedicineSourceMapper;
    @Resource
    private MmsMedicineMapper mmsMedicineMapper;

    /**
     * @description 删除药知源与药品关系
     *
     * @create 2023/12/5
     * @author dxn
     *
     * @param source_id  药知源文章id
     * @param medicine_id 药品id
    */
    public void deleteSourceAndMedicineRelation(String source_id,String medicine_id){
        if (isNullOrEmpty(source_id,medicine_id)){
            return;
        }
        QueryWrapper<MmsMedicineSourceMedicineRelation> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(source_id)){
            queryWrapper.lambda().eq(MmsMedicineSourceMedicineRelation::getSourceId,source_id);
        }
        if (!isNullOrEmpty(medicine_id)){
            queryWrapper.lambda().eq(MmsMedicineSourceMedicineRelation::getMedicineId,medicine_id);
        }

        relationMapper.delete(queryWrapper);
    }

    /**
     * @description 药知源与药品关系列表
     *
     * @create 2023/12/6
     * @author dxn
     *
     * @param id  药知源或药品id
     * @param idType  id类型  1：药品id   2：药知源id
    */
    public List<MmsMedicineSourceAndMedicineRelationResp> getRelationList(String id,Integer idType){
        List<MmsMedicineSourceAndMedicineRelationResp> relationList = new ArrayList<>();
        if (isNullOrEmpty(id)) {
            return relationList;
        }
        if (!idType.equals(ID_TYPE_MEDICINE) && !idType.equals(ID_TYPE_SOURCE)) {
            return relationList;
        }

        QueryWrapper<MmsMedicineSourceMedicineRelation> queryWrapper = new QueryWrapper<>();
        if (idType.equals(ID_TYPE_SOURCE)) {
            queryWrapper.lambda().eq(MmsMedicineSourceMedicineRelation::getSourceId, id);
        } else {
            queryWrapper.lambda().eq(MmsMedicineSourceMedicineRelation::getMedicineId, id);
        }
        List<MmsMedicineSourceAndMedicineRelationResp> list = relationMapper.selectList(queryWrapper)
                .stream()
                .filter(relationFilter -> {
                    if (idType.equals(ID_TYPE_SOURCE)) {
                        MmsMedicine medicine = mmsMedicineMapper.selectById(relationFilter.getMedicineId());
                        if (medicine == null || medicine.getIsOn().equals(MEDICINE_IS_OFF)) {
                            return false;
                        }
                    } else {
                        MmsMedicineSource medicineSource = mmsMedicineSourceMapper.selectById(relationFilter.getSourceId());
                        if (medicineSource == null || medicineSource.getIsDisplay().equals(IS_DISPLAY_FALSE)) {
                            return false;
                        }
                    }
                    return true;
                })
                .map(relationBean -> {
                    MmsMedicineSourceAndMedicineRelationResp relationResp = new MmsMedicineSourceAndMedicineRelationResp();
                    relationResp.setRelationId(relationBean.getId());
                    if (idType.equals(ID_TYPE_SOURCE)) {
                        MmsMedicine medicine = mmsMedicineMapper.selectById(relationBean.getMedicineId());
                        relationResp.setId(medicine.getId());
                        relationResp.setTitle(medicine.getCommonNames());
                        relationResp.setImg(medicine.getMainPic());
                        relationResp.setContent(medicine.getProductIntroduction());
                        relationResp.setMedicineCode(medicine.getMedicineCode());
                        relationResp.setSalesPrice(medicine.getSalesPrice());
                        relationResp.setMarketPrice(medicine.getMarketPrice());
                    } else {
                        MmsMedicineSource medicineSource = mmsMedicineSourceMapper.selectById(relationBean.getSourceId());
                        relationResp.setId(medicineSource.getId());
                        relationResp.setTitle(medicineSource.getSourceTitle());
                        relationResp.setImg(medicineSource.getSourceImg());
                        relationResp.setContent(medicineSource.getSourceContent());
                    }
                    return relationResp;
                })
                .collect(Collectors.toList());
        return list;
    }


}
